package com.goldplusgold.tradedelay.app.market.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.goldplusgold.support.lib.base.activity.BaseActivity;
import com.goldplusgold.tradedelay.app.R;
import com.goldplusgold.tradedelay.app.base.constant.IntentConstant;
import com.goldplusgold.tradedelay.app.market.fragment.QuotationChartFragment;
import com.goldplusgold.tradedelay.app.market.pojo.TDProductDetailInfo;
import com.goldplusgold.tradedelay.app.market.widget.TDProductDetailDialog;
import com.goldplusgold.tradedelay.app.market.widget.TDTypeSelectPopup;
import com.goldplusgold.tradedelay.app.base.listeners.Interface;
import com.goldplusgold.network.lib.http.protobuf.Market;
import com.goldplusgold.tradedelay.app.base.utils.ActivityUtils;
import com.goldplusgold.ui.lib.keyboard.KeyboardUtil;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;

public class QuotationChartActivity extends BaseActivity implements Handler.Callback {

    @BindView(R.id.iv_return)
    ImageView ivReturn;
    @BindView(R.id.title_type)
    TextView titleType;
    @BindView(R.id.txt_detail)
    TextView txtDetail;
    @BindView(R.id.title_layout)
    RelativeLayout titleLayout;
    @BindView(R.id.type_layout)
    LinearLayout typeLayout;
    @BindView(R.id.title_type_code)
    TextView titleTypeCode;
    @BindView(R.id.id_root_quotation_layout)
    RelativeLayout mRootView;
    @BindView(R.id.iv_complete)
    ImageView ivComplete;
    @BindView(R.id.iv_warn)
    ImageView ivWarn;
    @BindView(R.id.remind_info)
    TextView remindInfo;
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.remind_layout)
    RelativeLayout remindLayout;
    private Market.InstType mPosition = Market.InstType.AU_TD;
    private KeyboardUtil keyboardUtil;
    private Timer timer;
    private Handler handler;

    QuotationChartFragment fragment;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        handler = new Handler(this);
        if (getIntent() != null) {
            String typeCode = getIntent().getStringExtra(IntentConstant._TRADE_CODE);
            String tradeType = getIntent().getStringExtra(IntentConstant._TRADE_TYPE);
            titleType.setText(tradeType);
            titleTypeCode.setText(typeCode);
            switch (typeCode) {
                case "Au(T+D)":
                    mPosition = Market.InstType.AU_TD;
                    break;
                case "Ag(T+D)":
                    mPosition = Market.InstType.AG_TD;
                    break;
                default:
                    mPosition = Market.InstType.MAU_TD;
                    break;
            }
        }

        fragment = QuotationChartFragment.newInstance(mPosition);

        this.getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.quotation_chart_layout,
                        fragment)
                .addToBackStack(null)
                .commit();
        initMoveKeyBoard();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_tdquotation_chart;
    }

    private void initMoveKeyBoard() {
        keyboardUtil = new KeyboardUtil(this, mRootView);
    }

    public KeyboardUtil getKeyBoardUtil() {
        return keyboardUtil;
    }

    @OnClick({R.id.iv_close, R.id.iv_return, R.id.txt_detail, R.id.type_layout , R.id.type_layout_img})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                remindLayout.setVisibility(View.GONE);
                break;
            case R.id.iv_return:
                finish();
                break;
            case R.id.txt_detail:
                showTypeDetailDialog();
                break;
            case R.id.type_layout:
            case R.id.type_layout_img:
                TDTypeSelectPopup tdTypeSelectPopup = new TDTypeSelectPopup(QuotationChartActivity.this, mPosition);
                tdTypeSelectPopup.showAsDropDown(titleLayout);
                ActivityUtils.backgroundAlpha(QuotationChartActivity.this, 0.5f);
                tdTypeSelectPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {// 点击消失
                        ActivityUtils.backgroundAlpha(QuotationChartActivity.this, 1f);
                    }
                });
                tdTypeSelectPopup.setTDTypeClickListener(new Interface.OnTDTypeClickListener() {
                    @Override
                    public void onTypeSet(int position) {
                        if (position == 1) {
                            titleType.setText("黄金延期");
                            titleTypeCode.setText("Au(T+D)");
                            mPosition = Market.InstType.AU_TD;
                        } else if (position == 2) {
                            titleType.setText("白银延期");
                            titleTypeCode.setText("Ag(T+D)");
                            mPosition = Market.InstType.AG_TD;
                        } else {
                            titleType.setText("迷你黄金延期");
                            titleTypeCode.setText("mAu(T+D)");
                            mPosition = Market.InstType.MAU_TD;
                        }
                        if(fragment != null){
                            fragment.changeType(mPosition);
                        }

                    }
                });
                break;
        }
    }


    private void showTypeDetailDialog() {
        TDProductDetailInfo tdProductDetailInfo = new TDProductDetailInfo();
        switch (mPosition) {
            case AU_TD:
                tdProductDetailInfo.setTradeCode("Au(T+D)");
                tdProductDetailInfo.setTradeUnit("1000g/手");
                tdProductDetailInfo.setMinimumTradeVolume("1手");
                tdProductDetailInfo.setMaximumVolume("1000手");
                tdProductDetailInfo.setMarginLevel("11%");
                tdProductDetailInfo.setLowestMarginLevel("7%");
                tdProductDetailInfo.setLimitMovePercent("上一交易日结算价的±6%");
                tdProductDetailInfo.setFee("成交金额的0.08%");
                tdProductDetailInfo.setDelayCompensation("合约市值的0.0175%");
                break;
            case AG_TD:
                tdProductDetailInfo.setTradeCode("Ag(T+D)");
                tdProductDetailInfo.setTradeUnit("1000g/手");
                tdProductDetailInfo.setMinimumTradeVolume("1手");
                tdProductDetailInfo.setMaximumVolume("2万手");
                tdProductDetailInfo.setMarginLevel("13%");
                tdProductDetailInfo.setLowestMarginLevel("9%");
                tdProductDetailInfo.setLimitMovePercent("上一交易日结算价的±8%");
                tdProductDetailInfo.setFee("成交金额的0.08%");
                tdProductDetailInfo.setDelayCompensation("合约市值的0.0175%");
                break;
            case MAU_TD:
                tdProductDetailInfo.setTradeCode("mAu(T+D)");
                tdProductDetailInfo.setTradeUnit("100g/手");
                tdProductDetailInfo.setMinimumTradeVolume("1手");
                tdProductDetailInfo.setMaximumVolume("1万手");
                tdProductDetailInfo.setMarginLevel("11%");
                tdProductDetailInfo.setLowestMarginLevel("7%");
                tdProductDetailInfo.setLimitMovePercent("上一交易日结算价的±6%");
                tdProductDetailInfo.setFee("成交金额的0.08%");
                tdProductDetailInfo.setDelayCompensation("合约市值的0.0175%");
                break;
        }
        new TDProductDetailDialog(QuotationChartActivity.this, tdProductDetailInfo, R.style.MyDialog).show();
    }

    public void showRemindLayout(String remindTxt, int type) {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        if (type == 1) {
            ivComplete.setVisibility(View.VISIBLE);
            ivWarn.setVisibility(View.GONE);
        } else {
            ivComplete.setVisibility(View.GONE);
            ivWarn.setVisibility(View.VISIBLE);
        }
        remindLayout.setVisibility(View.VISIBLE);
        remindInfo.setText(remindTxt);
        int[] location = new int[2];
        int[] location1 = new int[2];
        remindLayout.getLocationOnScreen(location);
        remindLayout.getLocationInWindow(location1);
        TranslateAnimation translateAnimation = new TranslateAnimation(0, 0, -location[1] - location1[1], location[1] - location[1]);
        translateAnimation.setDuration(800);
        remindLayout.startAnimation(translateAnimation);
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.sendEmptyMessage(0);
            }
        }, 4000);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (keyboardUtil.isShow) {
            keyboardUtil.hideSystemKeyBoard();
            keyboardUtil.hideAllKeyBoard();
            keyboardUtil.hideKeyboardLayout();
        }
    }

    @Override
    public boolean handleMessage(Message msg) {

        switch (msg.what) {
            case 0:
                remindLayout.setVisibility(View.GONE);
                break;
        }
        return true;
    }
}
